home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1994 / MacHack 1994.toast / MacHack™94 / Miscellaneous / Randy Thelen / ThreadedSort / Sort ƒ / SortPicts2.cp < prev    next >
Encoding:
Text File  |  1994-06-26  |  2.1 KB  |  121 lines  |  [TEXT/MMCC]

  1. #include "SortPicts.h"
  2.  
  3. Boolean    SortPicts::PrepareGWorld( ConstStr255Param pictName)
  4. {
  5.     if( !LoadPicture( pictName))
  6.         return false;
  7.  
  8.     if( !MakeGWorld())
  9.     {
  10.         ReleaseResource( (Handle)sortPict);
  11.         return false;
  12.     }
  13.  
  14.     if( !AllocSortData())
  15.     {
  16.         ReleaseResource( (Handle)sortPict);
  17.         DisposeGWorld( sortGWorld);
  18.         return false;
  19.     }
  20.     
  21.     MakeSortData();
  22.  
  23.     return true;            //    YES!! The everything worked out!
  24. }
  25.  
  26. Boolean    SortPicts::LoadPicture( ConstStr255Param pictName)
  27. {
  28.     sortPict = (PicHandle)GetNamedResource( 'PICT', pictName);
  29.     
  30.     if( sortPict == nil)
  31.         return false;
  32.     
  33.     sortRect = (*sortPict)->picFrame;
  34.     pictWidth = sortRect.right - sortRect.left;
  35.     pictHeight = sortRect.bottom - sortRect.top;
  36.     
  37.     SetRect(   &sortRect, 0, 0, pictWidth, pictHeight);
  38.     copyBitsRect = sortRect;
  39.     windPictRect = sortRect;
  40.     windPictRect.top += kWindPictRectVoffset;
  41.     windPictRect.bottom += kWindPictRectVoffset;
  42.     
  43.     return true;
  44. }
  45.  
  46.  
  47. Boolean    SortPicts::MakeGWorld( void)
  48. {
  49.     OSErr            myOSErr;
  50.     GWorldPtr        tempSortGWorld;
  51.     GDHandle        oldGDevice;
  52.     CGrafPtr        oldGWorld;
  53.     Rect            tempSortRect;
  54.  
  55.     tempSortRect = sortRect;
  56.  
  57.     GetGWorld( &oldGWorld, &oldGDevice);
  58.     
  59.     myOSErr = NewGWorld( &tempSortGWorld, 8, &tempSortRect, 
  60.                         (CTabHandle) 0, (GDHandle) 0, keepLocal);
  61.     
  62.     if( myOSErr != noErr)
  63.         return false;
  64.             
  65.     sortGWorld = tempSortGWorld;
  66.     
  67.     SetGWorld( sortGWorld, (GDHandle)0);
  68.     
  69.     EraseRect( &sortRect);
  70.     DrawPicture( sortPict, &sortRect);
  71.     
  72.     SetGWorld( oldGWorld, oldGDevice);
  73.  
  74.     return true;    
  75. }
  76.  
  77.  
  78. Boolean    SortPicts::AllocSortData( void)
  79. {
  80.     sortPixmap = GetGWorldPixMap( sortGWorld);
  81.  
  82.     pictRowBytes = (*sortPixmap)->rowBytes & 0x3FFF;
  83.  
  84.     N = pictWidth * pictHeight;
  85.     
  86.     sortHandle = (SortDataHandle) NewHandle( N * sizeof(long) );
  87.                 
  88.     if( sortHandle == nil)
  89.         return false;
  90.  
  91.     return true;
  92. }
  93.  
  94.  
  95. void    SortPicts::MakeSortData( void)
  96. {
  97.     long            horiz;
  98.     long            loop;
  99.     SortPixel        pixel;
  100.     
  101.     UseSortData();
  102.  
  103.     horiz = 0;
  104.  
  105.     for( loop = 0; loop < N; ++loop)
  106.     {
  107.         pixel = sortPixels[horiz];
  108.         sortData[loop] = ((SortData) pixel) | (loop << 8);
  109.  
  110.         if( ++horiz == pictWidth)
  111.         {
  112.             horiz = 0;
  113.             sortPixels += pictRowBytes;
  114.         }
  115.     }
  116.  
  117.     UnuseSortData();    
  118. }
  119.  
  120.  
  121.